package com.codeavatar.hardwaretester.activity.hardware.bluetooth;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;

import com.codeavatar.catools.base.CaBaseActivity;
import com.codeavatar.catools.helper.CaActionBarHelper;
import com.codeavatar.catools.helper.CaLogHelper;
import com.codeavatar.catools.helper.CaViewHelper;
import com.codeavatar.catools.lib.tester.CaTimerLib;
import com.codeavatar.hardwaretester.R;
import com.codeavatar.hardwaretester.databinding.ActivityBluetoothConnectTesterBinding;
import com.codeavatar.hardwaretester.lib.BluetoothLib;

import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class ConnectTesterActivity extends CaBaseActivity {

    private final String TAG = this.getClass().getSimpleName();

    private ActivityBluetoothConnectTesterBinding mainBinding;

    private BluetoothLib bluetoothLib;
    private Timer timer;
    private int times = 10;

    private CaTimerLib caTimerTool;

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private void doInit(){
        if(null != gBundleExtras){
            this.bluetoothLib = new BluetoothLib(this);
            String deviceId = gBundleExtras.getString("device_id");
            UUID serviceUuid = UUID.fromString(gBundleExtras.getString("service_uuid"));
            UUID characteristicUuid = UUID.fromString(gBundleExtras.getString("characteristic_uuid"));
            this.bluetoothLib.doInit(deviceId,serviceUuid,characteristicUuid);
        }
    }

    private void setTestState(boolean enabled){
        mainBinding.btnxRestart.setEnabled(enabled);
    }

    private void doStartTest(){
        this.caTimerTool.start();
        if(null == this.timer){
            this.timer = new Timer();
        }
        this.setTestState(false);
        this.timer.schedule(timerTask,0,5000);
    }

    private void doStopTest(){
        if(null != this.timer){
            this.timer.cancel();
            this.timer.purge();
        }
        this.setTestState(true);
        this.caTimerTool.stop();
    }

    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            bluetoothLib.doConnect();
            times--;
            if(times<=0){
                doStopTest();
            }
        }
    };

    //++++++++++++++++++++++++++++++++++++++
    //++ 重写方法
    //++++++++++++++++++++++++++++++++++++++

    @Override
    protected void initBinding() {
        mainBinding = ActivityBluetoothConnectTesterBinding.inflate(getLayoutInflater());
    }

    @Override
    protected View getLayoutView() {
        return mainBinding.getRoot();
    }

    @Override
    protected void initNavigation() {
        CaActionBarHelper.init().toolbar(getSupportActionBar(),"蓝牙连接测试");
    }

    @Override
    protected void initView() {
        caTimerTool = new CaTimerLib();
    }

    @Override
    protected void initEvent() {
        CaViewHelper.init().setOnChildViewClick(mainBinding.lltxButtons,this);
    }

    @Override
    protected void initData() {
        this.doInit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnx_restart:
                this.doStartTest();
                break;
            case R.id.btnx_download:

                break;
            default:
                CaLogHelper.warn(TAG, getResources().getString(R.string.ca_txt_no_resid));
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if(android.R.id.home == itemId){
            this.finish();
        }else{
            return super.onOptionsItemSelected(item);
        }
        return true;
    }
}
